<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>websocket</title>
</head>
<body>
	<input id="message" type="text" name="message">
	<button onclick="sendMessage()">提交</button>
</body>
</html>

<script>
	// ws 全局变量
	var obj;
	ws = new WebSocket("ws://127.0.0.1:51345");
	ws.onopen=function(msg){
		ws.send('Hello!');
	}

	ws.onmessage=function(msg){
		console.log('message:');
		console.log(msg);
	}

	ws.onerror=function(e){
		console.log("error :" );
		console.log(e);
	}

	ws.onclose=function(){
		console.log('websocket close');
	}

	function sendMessage () {
		obj = document.getElementById("message");
	    ws.send(obj.value);
	    obj.value = "";
	}
</script>